
// import ImageCompressor from 'image-compressor.js';
// import { message } from 'ant-design-vue';
import { upload } from '@/api/oss';

// const OSS = require('ali-oss');

const UploadPost = (file) => {
  return new Promise((resolve, reject) => {
    upload(file).then(res => {
      resolve(res.data);
    }).catch(err => {
      reject(err);
    });
  });
};

export function UploadImage(file, resize) {
  const fileName = file.name;
  return new Promise((resolve, reject) => {
    // let fileName = '';
    // if (file.name) {
    //   const index = file.name.lastIndexOf('.');
    //   const type = file.name.substring(index);
    //   fileName = String(file.size) + String(Math.floor(Math.random() * 1000000)) + type;
    // } else {
    //   fileName = String(file.size) + String(Math.floor(Math.random() * 1000000)) + '.jpg';
    // }

    // eslint-disable-next-line
    // const resize = new ImageCompressor(file, {
    //   maxWidth: 4000,
    //   checkOrientation: true,
    //   success(fileCom) {
    //     const formData = new FormData();
    //     formData.append('file', fileCom, fileName);

    //     UploadPost(formData).then(res => {
    //       resolve(res);
    //     }).catch(err => {
    //       console.log(err);
    //       // message.info('上传失败！');
    //       reject(err);
    //     });
    //   }
    // });

    const formData = new FormData();
    formData.append('file', file, fileName);
    UploadPost(formData).then(res => {
      resolve(res);
    }).catch(err => {
      console.log(err);
      // message.info('上传失败！');
      reject(err);
    });
  });
}
